package com.njmt.cloud.msprovder.service.imp;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.njwt.cloud.mscommon.feign.client.AuthorityClient;
import com.njwt.cloud.mscommon.feign.client.UserClient;
import com.njwt.cloud.mscommon.pojo.bo.UserBo;
import com.njwt.cloud.mscommon.pojo.vo.PermissionVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import com.njwt.cloud.mscommon.support.ApiResponse;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author <a href="mailto:xingxiaofeng@gtmap.cn">xingxiaofeng</a>
 * @date 2022-06-02
 * @desc UserDetailsServiceImpl
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
     @Autowired
     private UserClient userClient;
     @Autowired
     private AuthorityClient authorityClient;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        if(StringUtils.isBlank(username)){
            //throw new Exception("登录名不能为空");
        }
        ApiResponse apiResponse = userClient.getUserByUserName(username);
      /*  if (!SophiaHttpStatus.SUCCESS.getCode().equals(apiResponse.getCode())){
            throw new CommonException(apiResponse.getMessage());
        }*/
        UserBo user = JSONObject.parseObject(JSONObject.toJSONString(apiResponse.getData(), true),UserBo.class);
     /*   if (user == null) {
            throw new CommonException("登录名不存在");
        } else if (BizConstants.USER_STATUS_EXPIRED.equals(user.getStatus())) {
            throw new CommonException("用户已过期");
        } else if (BizConstants.USER_STATUS_LOCKED.equals(user.getStatus())) {
            throw new CommonException("用户已锁定");
        } else if (BizConstants.USER_STATUS_UNUSED.equals(user.getStatus())) {
            throw new CommonException("用户已禁用");
        }*/
        ApiResponse response = authorityClient.getAuthorityByUserId(user.getId());
        List<PermissionVo> authList = JSON.parseArray(JSON.toJSONString(response.getData(), true), PermissionVo.class);
        Set<GrantedAuthority> lists = new HashSet<>();
        if(authList != null && authList.size()>0){
            for (PermissionVo auth : authList) {
                if (StringUtils.isNotBlank(auth.getCode())) {
                    lists.add(new SimpleGrantedAuthority(auth.getCode()));
                }
            }
        }
        User loginUser = new User(username,"123456",lists);
       /* loginUser.setId(user.getId());
        loginUser.setDeptId(user.getDeptId());
        loginUser.setCompId(user.getCompId());*/
        return loginUser;

      /*  Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
        boolean enabled = true; // 可用性 :true:可用 false:不可用
        boolean accountNonExpired = true; // 过期性 :true:没过期 false:过期
        boolean credentialsNonExpired = true; // 有效性 :true:凭证有效 false:凭证无效
        boolean accountNonLocked = true; // 锁定性 :true:未锁定 false:已锁定
        GrantedAuthority grantedAuthority = new SimpleGrantedAuthority("admin");
        grantedAuthorities.add(grantedAuthority);
        GrantedAuthority authority = new SimpleGrantedAuthority("params01");
        grantedAuthorities.add(authority);
        GrantedAuthority authority2 = new SimpleGrantedAuthority("getOrder");
        grantedAuthorities.add(authority2);
        GrantedAuthority authority3 = new SimpleGrantedAuthority("getuserinfo");
        grantedAuthorities.add(authority3);
        User user = new User(username, username,
                enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, grantedAuthorities);
        return user;*/
    }
}
